<template>
  <div class="pagination-box">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
  </div>
</template>

<script lang="ts">

  import { Vue, Component, Prop } from 'vue-property-decorator'

  @Component({
    name: 'WPagination'
  })
  export default class Pagination extends Vue{
    /**
     * 总数
     */
    @Prop({ type: Number, default: 0 }) total!: number

    /**
     * 当前页面
     */
    currentPage = 1
    /**
     * 分页大小
     */
    pageSize = 10

    mounted() {
      this.emitPage()
    }

    /**
     * 分页大小改变的时候调用
     */
    handleSizeChange(num: number) {
      this.currentPage = 1
      this.pageSize = num
      this.emitPage()
    }

    /**
     * 当前页面改变的时候调用
     */
    handleCurrentChange(num: number) {
      this.currentPage = num
      this.emitPage()
    }
    emitPage()  {
      this.$emit('pageChange', {
        pageSize: this.pageSize,
        pageNum: this.currentPage
      })
    }
    reload() {
      this.currentPage = 1
      this.emitPage()
    }
  }

</script>

<style scoped lang="scss">
.pagination-box {
  display: flex;
  padding-top: 20px;
  justify-content: flex-end;
}
</style>
